package com.njupt.Tree;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: wujiaming
 * @CreateTime: 2024/11/20 10:52
 * @Description: 199. 二叉树的右视图
 * @Version: 1.0
 */


public class RightSideView {

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> results = new ArrayList<>();
        Deque<TreeNode> queue = new LinkedList<>();
        if(root == null) return results;
        queue.addLast(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode treeNode = queue.pollFirst();
                if(i==size-1){
                   results.add(treeNode.val);
                }
                if(treeNode.left != null) queue.addLast(treeNode.left);
                if(treeNode.right != null) queue.addLast(treeNode.right);
            }
        }
        return results;

    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(5);
        root.right.right = new TreeNode(4);

        RightSideView test = new RightSideView();
        List<Integer> list = test.rightSideView(root);
        System.out.println(list);
    }
}
